package com.sec.etech.bpm.stat.controller;

import com.sec.etech.bpm.job.EtechTaskExtJob;
import com.sec.etech.bpm.stat.manager.BpmStatManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/bpm/stat2/"})
@Api(description = "流程统计接口")
public class BpmStatController {

    @Resource
    private BpmStatManager bpmStatisticsManager;

    @Autowired
    EtechTaskExtJob etechTaskExtJob;
    @RequestMapping(value = {"calTaskRemainDaysDaily"}, method = {RequestMethod.POST,RequestMethod.GET})
    @ApiOperation(value = "工作日计算统计", notes = "")
    public ResultMsg calTaskRemainDaysDaily(HttpServletRequest request, HttpServletResponse response) throws Exception {
        etechTaskExtJob.calTaskRemainDaysDaily();
        return new ResultMsg("ok");
    }

    @RequestMapping(value = {"queryFlowApplyCountsGroupByDefKey"}, method = {RequestMethod.POST})
    @ApiOperation(value = "流程申请数量统计", notes = "统计流程申请量 最多/最少 的前 20个流程")
    public ResultMsg queryFlowApplyCountsGroupByDefKey(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByDefKey");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApplyCounts(mapParam);
        return new ResultMsg(listStat);
    }

    @RequestMapping(value = {"queryApplyCountsGroupByOrg"}, method = {RequestMethod.POST})
    @ApiOperation(value = "部门流程申请数量统计", notes = "统计流程申请量 最多/最少 的前20个部门（可指定流程）")
    public ResultMsg queryApplyCountsGroupByOrg(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByOrg");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApplyCounts(mapParam);
        return new ResultMsg(listStat);
    }


    @RequestMapping(value = {"queryApplyCountsGroupByUser"}, method = {RequestMethod.POST})
    @ApiOperation(value = "用户流程申请数量统计", notes = "统计流程申请量 最多/最少 的前 20位用户（可指定流程）")
    public ResultMsg queryApplyCountsGroupByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByUser");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApplyCounts(mapParam);
        return new ResultMsg(listStat);
    }


    ////////////

    @RequestMapping(value = {"queryApproveCountsGroupByUser"}, method = {RequestMethod.POST})
    @ApiOperation(value = "用户任务办理数量统计", notes = "统计任务完成数量 最多/最少 的前20个用户")
    public ResultMsg queryApproveCountsGroupByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByUser");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApproveCounts(mapParam);
        return new ResultMsg(listStat);
    }
    @RequestMapping(value = {"queryApproveCountsGroupByGroup"}, method = {RequestMethod.POST})
    @ApiOperation(value = "部门任务办理数量统计", notes = "统计任务完成数量 最多/最少 的前20个部门")
    public ResultMsg queryApproveCountsGroupByGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByGroup");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApproveCounts(mapParam);
        return new ResultMsg(listStat);
    }


    ////////////


    @RequestMapping(value = {"queryApplyAvgTimesGroupByDefKey"}, method = {RequestMethod.POST})
    @ApiOperation(value = "流程申请平均耗时统计", notes = "统计流程申请平均耗时 最多/最少 的前 20个流程")
    public ResultMsg queryApplyAvgTimesGroupByDefKey(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByDefKey");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApplyAvgTimes(mapParam);
        return new ResultMsg(listStat);
    }
    @RequestMapping(value = {"queryApplyAvgTimesGroupByUser"}, method = {RequestMethod.POST})
    @ApiOperation(value = "用户流程申请平均耗时统计", notes = "统计流程申请平均耗时 最多/最少 的前20个部门（可指定流程）")
    public ResultMsg queryApplyAvgTimesGroupByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByUser");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApplyAvgTimes(mapParam);
        return new ResultMsg(listStat);
    }
    @RequestMapping(value = {"queryApplyAvgTimesGroupByGroup"}, method = {RequestMethod.POST})
    @ApiOperation(value = "部门流程申请平均耗时统计", notes = "统计流程申请平均耗时 最多/最少 的前20个部门（可指定流程）")
    public ResultMsg queryApplyAvgTimesGroupByGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByGroup");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApplyAvgTimes(mapParam);
        return new ResultMsg(listStat);
    }

    /////////////

    @RequestMapping(value = {"queryApproveAvgTimesGroupByNodeId"}, method = {RequestMethod.POST})
    @ApiOperation(value = "流程节点平均耗时统计", notes = "统计任务处理平均耗时 最多/最少 的前20个节点")
    public ResultMsg queryApproveAvgTimesGroupByNodeId(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByNodeId");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApproveAvgTimes(mapParam);
        return new ResultMsg(listStat);
    }
    @RequestMapping(value = {"queryApproveAvgTimesGroupByUser"}, method = {RequestMethod.POST})
    @ApiOperation(value = "用户任务处理平均耗时统计", notes = "统计任务处理平均耗时 最多/最少 的前20个用户")
    public ResultMsg queryApproveAvgTimesGroupByUser(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByUser");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApproveAvgTimes(mapParam);
        return new ResultMsg(listStat);
    }
    @RequestMapping(value = {"queryApproveAvgTimesGroupByGroup"}, method = {RequestMethod.POST})
    @ApiOperation(value = "部门任务处理平均耗时统计", notes = "统计任务处理平均耗时 最多/最少 的前20个部门")
    public ResultMsg queryApproveAvgTimesGroupByGroup(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "groupByGroup");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryApproveAvgTimes(mapParam);
        return new ResultMsg(listStat);
    }

    /////////////

    @RequestMapping(value = {"queryInstanceFinishedRate"}, method = {RequestMethod.POST})
    @ApiOperation(value = "流程实例办结率变化", notes = "指定时间范围新发起流程与完成流程实例之间的比率")
    public ResultMsg queryInstanceFinishedRate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "queryInstanceFinishedRate");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryFinishedRate(mapParam);
        return new ResultMsg(listStat);
    }
    @RequestMapping(value = {"queryTaskFinishedRate"}, method = {RequestMethod.POST})
    @ApiOperation(value = "任务办结率变化", notes = "指定时间范围新创建任务与完成任务之间的比率")
    public ResultMsg queryTaskFinishedRate(HttpServletRequest request, HttpServletResponse response) throws Exception {
        Map mapParam = getQueryParamMap(request);
        mapParam.put("statKey", "queryTaskFinishedRate");
        mapParam.put("limit", "20");
        List<Map<String, Object>> listStat = bpmStatisticsManager.queryFinishedRate(mapParam);
        return new ResultMsg(listStat);
    }



    private Map<String, String> getQueryParamMap(HttpServletRequest request){
        Map mapParam = new HashMap<String, Object>();
        String[] paramNames = {"defKey", "orgId","startTime","endTime","orderBy"};
        for(String name: paramNames){
            if(StringUtil.isNotEmpty(request.getParameter(name))){
                mapParam.put(name, request.getParameter(name));
            }
        }
        return mapParam;
    }


}
